package com.wyz.saas.app.mvvm.list

import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.viewModelScope
import com.atom.module.mvvm.base.AbstractBaseViewModel
import com.atom.module.mvvm.base.IModel
import com.atom.module.mvvm.base.AbstractViewModel
import com.atom.module.mvvm.livedata.SingleLiveData
import com.wyz.saas.app.net.ApiService
import com.wyz.saas.app.net.NewsCard
import com.wyz.saas.common.base.AbstractApiViewModel
import com.wyz.saas.common.hateoas.ApiNetwork
import com.wyz.saas.common.impl.EmptyApiModel
import kotlinx.coroutines.launch

class ListViewModel : AbstractApiViewModel<EmptyApiModel>() {

    val listdata = object : SingleLiveData<NewsCard>() {}

    lateinit var newsApiService: ApiService

    override fun onCreate(owner: LifecycleOwner) {
        super.onCreate(owner)
        newsApiService = ApiNetwork.remote("http://v.juhe.cn/", ApiService::class.java)
    }

    override fun onStart(owner: LifecycleOwner) {
        super.onStart(owner)
        viewModelScope.launch {
            loadingAction.post(true)
            listdata.post(newsApiService.getNews().result)
            loadingAction.post(false)
        }
    }

    fun loadMoreNewsData(page: Int, page_size: Int = 30) {
        viewModelScope.launch {
            listdata.post(newsApiService.getNews(page = page, page_size = page_size).result)
        }
    }
}